<template>
    <div class="data-manager-panal">
        <timeline v-if="times.length > 0" :times="times"></timeline>
    </div>
</template>
<script>
/* global Cesium */
import { getToken, getAllTimeService, getTimeServiceParams } from 'S/production';
import { flyToBoundingSphere } from 'U/dataQueryFunc';
// import { initImagerLayer, addImageryLayerWithTime } from 'U/imageryLayerFun';
import { addImageryLayerWithTime } from 'U/imageryLayerFun';
import timeline from './timeline';

export default {
    name: 'dataManager',
    props: {
        typeCode: String,
        serviceUrl: String,
    },
    components: {
        timeline,
    },
    data() {
        return {
            times: [],
            currentLayer: undefined,
        };
    },
    mounted() {
        // initImagerLayer();
    },
    beforeDestroy() {
        addImageryLayerWithTime(false);
    },
    methods: {
        async initImg(nodeName) {
            let token = await getToken(this.serviceUrl);
            localStorage.setItem('Authorization', token.data.token);
            let res = await getAllTimeService();
            this.imgArr = res.data.data;
            let eleImg = this.imgArr.filter(item => item.layerName === nodeName);
            this.addImageryLayer(eleImg[0], status);
        },
        async addImageryLayer(data) {
            let serviceParams = {};
            let param = {
                layerName: data.layerName,
                // lat: (data.minY + data.maxY) / 2,
                // lon: (data.minX + data.maxX) / 2,
                // request: 'gettime',
                // zoo: data.minLevel,
            };
            if (this.$parent.themeParam && this.$parent.themeParam.nowChoice) {
                console.log('---');
            } else {
                flyToBoundingSphere(
                    [
                        Cesium.Cartesian3.fromDegrees(76.334472, 5.277985),
                        Cesium.Cartesian3.fromDegrees(153.443746, 49.113125),
                    ],
                    2
                );
                // flyToBoundingSphere(
                //     [
                //         Cesium.Cartesian3.fromDegrees(data.minX, data.minY),
                //         Cesium.Cartesian3.fromDegrees(data.maxX, data.maxY),
                //     ],
                //     3
                // );
            }

            serviceParams = await getTimeServiceParams(param);

            this.times = [];
            serviceParams.data.forEach(item => {
                item.timeN =
                    item.time.slice(0, 4) +
                    '-' +
                    item.time.slice(4, 6) +
                    '-' +
                    item.time.slice(6, 8);
                this.times.push(item.timeN);
            });
            let paramI = {
                layerData: serviceParams.data,
                times: this.times,
            };
            this.$emit('getTimes', this.times);
            addImageryLayerWithTime(true, data, paramI);
        },
    },
    watch: {
        typeCode: {
            handler(newV, oldV) {
                this.initImg(newV);
            },
            deep: true,
            immediate: true,
        },
    },
};
</script>
<style lang="scss">
.data-manager-panal {
    $header-height: 42px;
    $close-img: 14px;
    position: relative;
    left: 50px;
    top: -78px;
    width: 340px;
    background-color: #212b35;
}
</style>
